﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace minimus
{
    public partial class FormMain : Form
    {
        
        public FormMain()
        {
            InitializeComponent();
            trayIcon.Text = Application.ProductName + " " + Application.ProductVersion;
            label2.DataBindings.Add("Text", trackBar1, "Value", true);
            if (Properties.Settings.Default.EnableDebugging)
            {
                Trace.AutoFlush = true;
                Trace.Listeners.Add(new TextWriterTraceListener("debug.txt"));
            }
            SendTimer.Interval = Properties.Settings.Default.RequestInterval;

            m_GameRequester = new GHostGameRequester(Properties.Settings.Default.RemoteServer, (int)Properties.Settings.Default.RemotePort);
            Properties.Settings.Default.PropertyChanged += new PropertyChangedEventHandler(Default_PropertyChanged);
            //Create Handle because we want to use Invoke() before showing the form which would normally create the handle?
            //CreateHandle();
            //CreateControl();
        }

        void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "RemoteServer")
                m_GameRequester.RemoteServer = Properties.Settings.Default.RemoteServer;
            else if (e.PropertyName == "RemotePort")
            {
                m_GameRequester.RemotePort = (int)Properties.Settings.Default.RemotePort;
            }
            else if (e.PropertyName == "RequestInterval")
                SendTimer.Interval = Properties.Settings.Default.RequestInterval;
        }


        GHostGameRequester m_GameRequester;
        
        private void SendTimer_Tick(object sender, EventArgs e)
        {
            if (!Properties.Settings.Default.DetectLANScreenActive || WarcraftLANScreenActive())
            {
                Trace.WriteLine("Requesting gamelist");
                m_GameRequester.RequestGames();
            }
        }

        private bool WarcraftLANScreenActive()
        {
            try
            {
                Socket m_TestForWC3Sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                m_TestForWC3Sock.Bind(new IPEndPoint(IPAddress.Any, 6112));
                m_TestForWC3Sock.Close();
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.AddressAlreadyInUse)
                {
                    return true;
                }
            }
            return false;
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            trayIcon.Dispose();
            Application.Exit();
        }

        private void trayIcon_DoubleClick(object sender, EventArgs e)
        {
            Visible = !Visible;
            if (Visible)
            {
                WindowState = FormWindowState.Normal;
                BringToFront();
                Activate();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                Hide();
            }
        }
    }
}
